/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EnrollmentDialog.java
 *
 * Created on 16.5.2010, 21:59:33
 */

package pv1682010.gui;

import java.awt.Frame;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.swing.JDialog;
import pv1682010.Course;
import pv1682010.CourseManager;
import pv1682010.Database;
import pv1682010.UserManager;

/**
 *
 * @author Isamaru
 */
public class EnrollmentDialog extends JDialog {

    private final DataSource ds;
    private final UserManager um;
    private final CourseManager cm;
    private final Course course;

    /** Creates new form EnrollmentDialog */
    public EnrollmentDialog(Frame parent, DataSource ds, Course course) {
        super(parent);
	this.ds = ds;
	this.course = course;
	um = new UserManager(ds);
	cm = new CourseManager(ds);
	initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        enrollmentTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pv1682010/gui/translations"); // NOI18N
        closeButton.setText(bundle.getString("close")); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        enrollmentTable.setModel(new EnrollmentTableModel(um, cm, course));
        jScrollPane1.setViewportView(enrollmentTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(414, Short.MAX_VALUE)
                .addComponent(closeButton)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
	setVisible(false);
	dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
		DataSource ds = new Database();
		CourseManager cm = new CourseManager(ds);
                EnrollmentDialog dialog;
		Course course;
		try {
		    course = cm.getAllCourses().get(0);
		} catch (SQLException ex) {
		    throw new RuntimeException(ex);
		}
		dialog = new EnrollmentDialog(new javax.swing.JFrame(), ds, course);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JTable enrollmentTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
